#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// 发送温湿度数据的线程函数
void* send_temperature_data(void* arg) {
    int socket_fd = *(int*)arg;

    // FILE *file = fopen("1.txt", "r");
    // if (file == NULL) {
    //     perror("Failed to open file");
    //     return NULL;
    // }

    // char buffer[1024];
    // while (fgets(buffer, sizeof(buffer), file) != NULL) {
    //     if (send(socket_fd, buffer, strlen(buffer), 0) < 0) {
    //         perror("Failed to send data");
    //         break;
    //     }
    // }

    // fclose(file);
    FILE* pf = fopen("1.txt", "r");
    long long  msg[3] = {0};
	long long da=0;
	float tm=0;
	float hu=0;
    if (pf != NULL)
	{
		while (fscanf(pf, "%lld %f %f", &da, &tm, &hu) != EOF) {
        // 在这里处理日期和时间
        msg[0]=da;
        msg[1]=tm;
        msg[2]=hu;
        if(send(socket_fd, msg, sizeof(msg), 0) < 0) {
        perror("send failed");
        exit(-2);
    }
        printf("消息已发送\n");
    }
	}else{
        perror("open\n");
        exit(-1);
    }    


    return NULL;
}

// 处理用户注册和登录的线程函数
void* handle_user_login(void* arg) {
    int socket_fd = *(int*)arg;

    char choice[10];
    printf("Do you want to register a new account? (yes/no): ");
    fgets(choice, sizeof(choice), stdin);

    char username[100];
    char password[100];

    if (strncmp(choice, "yes", 3) == 0) {
        printf("Enter username to register: ");
        fgets(username, sizeof(username), stdin);
        username[strcspn(username, "\n")] = 0; // remove newline character

        printf("Enter password to register: ");
        fgets(password, sizeof(password), stdin);
        password[strcspn(password, "\n")] = 0; // remove newline character

        char registerData[200];
        sprintf(registerData, "register: %s password: %s", username, password);

        if (send(socket_fd, registerData, strlen(registerData), 0) < 0) {
            perror("Failed to send register data");
        }
    } else {
        printf("Enter username: ");
        fgets(username, sizeof(username), stdin);
        username[strcspn(username, "\n")] = 0; // remove newline character

        printf("Enter password: ");
        fgets(password, sizeof(password), stdin);
        password[strcspn(password, "\n")] = 0; // remove newline character

        char loginData[200];
        sprintf(loginData, "login: %s password: %s", username, password);

        if (send(socket_fd, loginData, strlen(loginData), 0) < 0) {
            perror("Failed to send login data");
        }
    }

    return NULL;
}

int main(int argc, char *argv[]) {
    // 创建套接字并连接到服务器
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_fd < 0) {
        perror("socket creation failed");
        return -1;
    }

    struct sockaddr_in srvaddr;
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(55555);
    srvaddr.sin_addr.s_addr = inet_addr("192.168.64.7");

    if(connect(socket_fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) < 0) {
        perror("connect failed");
        return -1;
    }

    printf("连接成功\n");

    // 创建处理用户注册和登录的线程
    pthread_t thread1;
    pthread_create(&thread1, NULL, handle_user_login, &socket_fd);

    // 等待线程结束
    pthread_join(thread1, NULL);

    // 创建发送温湿度数据的线程
    pthread_t thread2;
    pthread_create(&thread2, NULL, send_temperature_data, &socket_fd);

    // 等待线程结束
    pthread_join(thread2, NULL);

    // 关闭套接字
    close(socket_fd);

    return 0;
}